<template>
  <div class="dashboard-editor-container">
    <div>
      <el-card>
        <el-form :model="ruleForm" style="margin-top:10px;" size="mini" label-position="top" ref="ruleForm">
          <el-row :gutter="10">
            <el-col :lg="4" :md="6">
              <el-form-item label="评分模板名称" prop='SCORETEMPLATENAME'>
                <el-input size="mini" placeholder="请输入评分模板名称" v-model="ruleForm.SCORETEMPLATENAME"></el-input>
              </el-form-item>
            </el-col>
            <!-- <el-col :lg="4" :md="6">
              <el-form-item label="业务类型" prop='BUSINESSTYPE'>
                <el-input size="small" placeholder="请输入业务类型" v-model="ruleForm.BUSINESSTYPE"></el-input>
              </el-form-item>
            </el-col> -->
            <!-- <el-col :lg="4" :md="6">
              <el-form-item label="渠道类型" prop='CHANNELTYPE'>
                <el-select v-model="ruleForm.CHANNELTYPE" filterable clearable placeholder="请选择业务渠道">
                  <el-option v-for="item in channelOption" :key="item.FIELDKEY" :label="item.FIELDVALUE"
                    :value="item.FIELDKEY"></el-option>
                </el-select>
              </el-form-item>
            </el-col> -->
            <el-col :lg="4" :md="6">
              <el-form-item label="创作人" prop='CREATIVEPERSON'>
                <el-input size="mini" placeholder="请输入创作人" v-model="ruleForm.CREATIVEPERSON"></el-input>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item label="评分指标大项" prop='SCOREMAJORITEMNAME'>
                <el-select size="mini" v-model="ruleForm.SCOREMAJORITEMNAME" clearable placeholder="请输入评分指标大项">
                  <el-option v-for="item in scoreOptionBig" :key="item.SCOREMAJORITEMID"
                    :label="item.SCOREMAJORITEMNAME" :value="item.SCOREMAJORITEMID"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="4" :md="6">
              <el-form-item label="评分指标小项" prop='SCOREMINORTERMNAME'>
                <el-select size="mini" v-model="ruleForm.SCOREMINORTERMNAME" clearable placeholder="请输入评分指标小项">
                  <el-option v-for="item in scoreOptionSmall" :key="item.SCOREMINORTERMID"
                    :label="item.SCOREMINORTERMNAME" :value="item.SCOREMINORTERMID" ></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :lg="8" :md="12" class="searchBtnMarginTop">
              <el-button type="primary" icon="el-icon-search" :loading="loading" size="mini" @click="queryTemplate">查询
              </el-button>
              <el-button type='warning' size='mini' icon='el-icon-refresh' @click="clearForm('ruleForm')">重置</el-button>
              <el-button type="success" size="mini" icon="el-icon-plus" @click="openEditDialog('','add')">新增模板
              </el-button>
            </el-col>
          </el-row>
        </el-form>
        <el-table :data="tableData" class="tableMarginTop">
          <el-table-column prop="SCORETEMPLATEID" align="center" width="120" label="模板编号"></el-table-column>
          <el-table-column prop="SCORETEMPLATENAME" align="center" label="模板名称"></el-table-column>
          <!-- <el-table-column prop="BUSINESSTYPE" align="center" label="业务类型"></el-table-column> -->
          <!-- <el-table-column prop="CHANNELTYPE" align="center" label="渠道类型"></el-table-column> -->
          <el-table-column prop="SCORETEMPLATENAME" align="center" label="创作人员"></el-table-column>
          <el-table-column prop="REVISINGPERSON" align="center" label="编辑人员"></el-table-column>
          <el-table-column prop="LASTREVISIONDATE" align="center" label="修改日期"></el-table-column>
          <el-table-column label="详情" align="center">
            <template slot-scope="scope">
              <div>
                <el-button type="primary" size="mini" @click="openEditDialog(scope.row,'detail')" icon="el-icon-search">
                  详情
                </el-button>
              </div>
            </template>
          </el-table-column>
          <el-table-column label="操作" align="center" width="200">
            <template slot-scope="scope">
              <div>
                <el-button type="warning" size="mini" icon="el-icon-edit" @click="openEditDialog(scope.row,'update')">
                  修改</el-button>
                <el-button type="danger" size="mini" icon="el-icon-delete" @click="delTemplate(scope.row)">删除
                </el-button>
              </div>
            </template>
          </el-table-column>
        </el-table>
        <el-row>
          <el-col>
            <el-pagination style="text-align: right; margin-top:20px;" :page-sizes="[10, 20, 50, 100]"
              :page-size="pageSize" @size-change="handleInSizeChange" @current-change="handleInCurrentChange"
              :current-page="currpage" background layout="sizes, total, prev, pager, next, jumper" :total="tableTotal">
            </el-pagination>
          </el-col>
        </el-row>
      </el-card>
    </div>
    <component :is="dialogList.editDialog" :option="editOptions" @refreshTable="queryTemplate"></component>
  </div>
</template>

<script>
  // 发送接口
  import getGlobalParams from "@/common/utils/getGlobalParams";
  import request from "@/common/utils/requestQC";
  import queyTemp from "@/message/QCManage/scoreTemplate/scoreTemplate/scoreTemplateQuery";
  import queyTempDetail from "@/message/QCManage/scoreTemplate/scoreTemplate/scoreTemplateDetailQuery";
  import delTemp from "@/message/QCManage/scoreTemplate/scoreTemplate/scoreTemplateDel";
  import toolBox from "@/common/utils/toolBox";
  import editDialog from "./dialog/editDialog";
  import dictionary from '@/message/wfm/baseData/dictionaryManage/dictionaryQuery'
  import requestWFM from "@/common/utils/requestWFM";
  import scoreDictionarySmallQuery from '@/message/QCManage/scoreTemplate/scoreDictionarySmall/scoreDictionarySmallQuery';
  import scoreDictionaryBigQuery from '@/message/QCManage/scoreTemplate/scoreDictionaryBig/scoreDictionaryBigQuery';
  //批量上传
  export default {
    name: '',
    data() {
      return {
        // channelOption: [], //渠道类型
        scoreOptionSmall: [], //评分子项
        scoreOptionBig: [], //评分大项
        dialogList: {
          editDialog
        },
        editOptions: {
          isShow: false,
          titile: '',
          row: {},
          flag: ''
        },
        tableTotal: 1,
        currpage: 1,
        pageSize: getGlobalParams.get("PageSize"),
        tableData: [],
        ruleForm: {
          SCORETEMPLATENAME: '', //模板名称
          // BUSINESSTYPE: '', //业务类型
          // CHANNELTYPE: '', //渠道类型
          CREATIVEPERSON: '', //创作人
          SCOREMAJORITEMNAME: '', //评分指标大项
          SCOREMINORTERMNAME: '' //评分指标小项
        },
        loading: false,
      }
    },
    computed: {},
    created() {

    },
    mounted() {
      this.queryTemplate()
      this.dataQueryBig()
      this.dataQuerySmall()
      // this.dictionaryQuery()
    },
    watch: {},
    methods: {
       // 查询渠道类型
      async dictionaryQuery() {
        let resBody = new dictionary();
        resBody.data.FIELDCODE = 'CHANNELTYPE'
        resBody.data.CurrPage = '1'
        resBody.data.PageRowNum = '100'
        let response = await requestWFM(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.channelOption = response.BODY.FIEDATA;
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          })
        }
      },
      findType(type,arr){
        let test = []
        arr.forEach(item=>{
          if(item.type == type){
            test.push(item)
          }
        })
        return test
      },
      //查看详情评分模板详细查询
      async openEditDialog(row, flag) {
        // this.editOptions.channelOption = this.channelOption
        this.editOptions.scoreOptionSmall = this.scoreOptionSmall
        this.editOptions.scoreOptionBig = this.scoreOptionBig
        let arrBig = []
        let arrSmall = []
        this.scoreOptionSmall.forEach(item => {
          arrSmall.push({
            value: item.SCOREMINORTERMID,
            label: item.SCOREMINORTERMNAME,
            type:item.SPECIALATTRIBUTE
          })
        })

        this.scoreOptionBig.forEach(item => {
          arrBig.push({
            value: item.SCOREMAJORITEMID,
            label: item.SCOREMAJORITEMNAME,
            type:item.SPECIALATTRIBUTE,
            children: this.findType(item.SPECIALATTRIBUTE,arrSmall)
          })
        })
        this.editOptions.options = arrBig
        this.editOptions.flag = flag
        if (flag == 'add') {
          this.editOptions.isShow = true
          this.editOptions.title = '新增评分模板'
        } else {
          let resBody = new queyTempDetail();
          resBody.data.SCORETEMPLATEID = row.SCORETEMPLATEID
          let response = await request(resBody);
          if (response.SYS_HEAD.ReturnCode == "000000") {
            this.editOptions.isShow = true
            this.editOptions.title = flag == 'detail' ? '查看详情' : '修改模板'
            this.editOptions.row = response.BODY
          } else {
            this.$message({
              type: "error",
              message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
            })
          }
        }
      },
      // 查询评分大项
      async dataQueryBig() {
        let resBody = new scoreDictionaryBigQuery();
        resBody.data.CurrPage = 1
        resBody.data.PageRowNum = 500
        let response = await request(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.scoreOptionBig = response.BODY.REQBODY;
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          })
        }
      },
      // 查询评分子项
      async dataQuerySmall() {
        let resBody = new scoreDictionarySmallQuery();
        resBody.data.CurrPage = 1
        resBody.data.PageRowNum = 500
        let response = await request(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.scoreOptionSmall = response.BODY.REQBODY;
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          })
        }
      },
      //前端分页
      handleInCurrentChange(val) {
        this.currpage = val;
        // console.log(`当前页: ${val}`);
        this.queryTemplate()
      },
      handleInSizeChange(val) {
        this.pageSize = val;
        this.queryTemplate()
      },
      //删除评分模板
      delTemplate(row) {
        this.$confirm('此操作将删除该模板, 是否继续?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(async () => {
          let resBody = new delTemp()
          resBody.data.SCORETEMPLATEID = row.SCORETEMPLATEID
          let response = await request(resBody);
          if (response.SYS_HEAD.ReturnCode == "000000") {
            this.$message({
              type: 'success',
              message: '删除成功!'
            });
            this.queryTemplate()
          } else {
            this.$message({
              type: "error",
              message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
            })
          }
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      },
      // 查询评分模板
      async queryTemplate() {
        let resBody = new queyTemp()
        resBody.data.SCORETEMPLATENAME = this.ruleForm.SCORETEMPLATENAME
        // resBody.data.BUSINESSTYPE = this.ruleForm.BUSINESSTYPE
        // resBody.data.CHANNELTYPE = this.ruleForm.CHANNELTYPE
        resBody.data.CREATIVEPERSON = this.ruleForm.CREATIVEPERSON
        resBody.data.SCOREMAJORITEMNAME = this.ruleForm.SCOREMAJORITEMNAME
        resBody.data.SCOREMINORTERMNAME = this.ruleForm.SCOREMINORTERMNAME
        resBody.data.PageFlag = 4
        resBody.data.CurrPage = this.currpage;
        resBody.data.PageRowNum = this.pageSize;
        let response = await request(resBody);
        if (response.SYS_HEAD.ReturnCode == "000000") {
          this.tableData = response.BODY.REQBODY
          this.tableTotal = response.BODY.rtnum;
        } else {
          this.$message({
            type: "error",
            message: "请求失败，错误码:" + response.SYS_HEAD.ReturnCode + "，错误信息：" + response.SYS_HEAD.ReturnMessage
          })
        }
      },
      changePage(val) {
        this.currentPage = val;
      },
      handleSizeChange(val) {
        this.pageSize = val
      },
      // 重置查询条件
      clearForm(item) {
        // 重置表格
        this.$refs["" + item].resetFields();
      },
    }
  }

</script>

<style rel="stylesheet/scss" lang="scss" scoped>
  table {
    border-collapse: collapse;
  }

  table,
  td,
  th {
    border: 2px solid #999;
  }

  td,
  th {
    width: 100px;
    height: 40px;
  }

</style>
